﻿using System;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        private string iniPath = "./Setting.ini";
        public Form2()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            OperateIniFile F_operateIni = new OperateIniFile();

            bool result = true;
            result = F_operateIni.WriteIniData("COM", "PortName", comboBox1.Text, iniPath);
            result = F_operateIni.WriteIniData("COM", "BaudRate", comboBox2.Text, iniPath);
            result = F_operateIni.WriteIniData("COM", "Parity", comboBox3.Text, iniPath);
            result = F_operateIni.WriteIniData("COM", "DataBits", comboBox4.Text, iniPath);
            result = F_operateIni.WriteIniData("COM", "StopBits", comboBox5.Text, iniPath);
            if (result == true)
            {
                MessageBox.Show("保存成功!");
            }
            else
            {
                MessageBox.Show("保存失败!");
            }

        }

        private void Form2_Load(object sender, EventArgs e)
        {

            OperateIniFile F_operateIni = new OperateIniFile();

            comboBox1.Text = F_operateIni.ReadIniData("COM", "PortName", iniPath);
            comboBox2.Text = F_operateIni.ReadIniData("COM", "BaudRate", iniPath);
            comboBox3.Text = F_operateIni.ReadIniData("COM", "Parity", iniPath);
            comboBox4.Text = F_operateIni.ReadIniData("COM", "DataBits", iniPath);
            comboBox5.Text = F_operateIni.ReadIniData("COM", "StopBits", iniPath);

            Rmax.Value = decimal.Parse(F_operateIni.ReadIniData("MEASURE", "Rmax", iniPath));
            Rmin.Value = decimal.Parse(F_operateIni.ReadIniData("MEASURE", "Rmin", iniPath));
            Vmax.Value = decimal.Parse(F_operateIni.ReadIniData("MEASURE", "Vmax", iniPath));
            Vmin.Value = decimal.Parse(F_operateIni.ReadIniData("MEASURE", "Vmin", iniPath));
            string Cellmodel= F_operateIni.ReadIniData("MEASURE", "Cellmodel", iniPath);
            string[] grid_mode=Cellmodel.Split(',');
            int no = 0;


          

            foreach (string i in grid_mode) 
            {
                DataGridViewRow dr = new DataGridViewRow();
                dr.CreateCells(dataGridView1);

                dr.Cells[0].Value = ++no;

                dr.Cells[1].Value = i.ToString();

                this.dataGridView1.Rows.Add(dr);
                this.dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];

            }
           
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            OperateIniFile F_operateIni = new OperateIniFile();
            bool result = true;
            result = F_operateIni.WriteIniData("MEASURE", "Rmax", Rmax.Value.ToString(), iniPath);
            result = F_operateIni.WriteIniData("MEASURE", "Rmin", Rmin.Value.ToString(), iniPath);
            result = F_operateIni.WriteIniData("MEASURE", "Vmax", Vmax.Value.ToString(), iniPath);
            result = F_operateIni.WriteIniData("MEASURE", "Vmin", Vmin.Value.ToString(), iniPath);

            if (result == true)
            {
                MessageBox.Show("保存成功!");
            }
            else
            {
                MessageBox.Show("保存失败!");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label12_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

            string cellmodel ="";
            for (int i = 0; i < dataGridView1.RowCount-1; i++)
            {
                if (i > -1)
                   
                cellmodel += dataGridView1.Rows[i].Cells[1].Value.ToString().Trim();
                cellmodel += ",";
            }
             cellmodel = cellmodel.Substring(0, cellmodel.Length - 1);

            OperateIniFile F_operateIni = new OperateIniFile();
            bool result = true;
            result = F_operateIni.WriteIniData("MEASURE", "Cellmodel", cellmodel, iniPath);
            
            if (result == true)
            {
                MessageBox.Show("保存成功!");
            }
            else
            {
                MessageBox.Show("保存失败!");
            }
        }
    }
}
